# -*- coding: utf-8 -*-
"""
超市销售数据分析
实验16 分析促销商品和非促销商品销售金额的周环比增长

@author: tange
"""

import pandas as pd

import matplotlib.pyplot as plt

# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['KaiTi']  # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题

data = pd.read_csv(r'C:\大数据分析综合实训-附件.csv', encoding='gbk')

# 数据预处理
task16_data = data[['销售金额', '销售日期', '是否促销']]

# 各周日期区间
week_range = [
    [20150101, 20150107],
    [20150108, 20150114],
    [20150115, 20150121],
    [20150122, 20150128]
]

# 促销商品的销售金额的周环比增长
bar_data_促销 = []
week0 = 0
for day in week_range:
    week1 = task16_data[task16_data['是否促销'] == '是'][task16_data['销售日期'] > day[0]][task16_data['销售日期'] < day[1]].销售金额.sum()
    bar_data_促销.append((week1 - week0)/week0)
    week0 = week1

# 绘制图表
fig, ax = plt.subplots()
ax.bar([2, 3, 4], bar_data_促销[1:])

# 非促销商品的销售金额的周环比增长
bar_data_非促销 = []
week0 = 0
for day in week_range:
    week1 = task16_data[task16_data['是否促销'] == '否'][task16_data['销售日期'] > day[0]][task16_data['销售日期'] < day[1]].销售金额.sum()
    bar_data_非促销.append((week1 - week0)/week0)
    week0 = week1

# 绘制图表
fig, ax = plt.subplots()
ax.bar([2, 3, 4], bar_data_非促销[1:])
